package com.nuanshui.heatedloan.util.dwz;

import java.util.ArrayList;
import java.util.List;

import com.nuanshui.heatedloan.extension.jpasearch.FieldSort;

/**
 * dwz分页参数
 */
public class DwzPage {
	/**
	 * 排序的常量
	 */
	public final static String ORDER_DIRECTION_ASC = "ASC";
	public final static String ORDER_DIRECTION_DESC = "DESC";

	/**
	 * 判断是否进入高级搜索的常量
	 */
	public final static String SEARCH_TRUE = "true";
	public final static String SEARCH_FALSE = "false";

	/**
	 * 默认每页记录数
	 */
	private static final int DEFAULT_PAGE_SIZE = 20;// 每页条数

	private  List<FieldSort> sortFields = new ArrayList<FieldSort>();

	/**
	 * 原始页码
	 */
	private int plainPageNum = 1;

	/**
	 * 当前页码
	 */
	private int pageNum = 1;// 当前页数
	private int numPerPage = DEFAULT_PAGE_SIZE;// 每页条数
	private String orderField = "";// 排序字段
	private String orderDirection = ORDER_DIRECTION_DESC;// 升序降序

	/**
	 * 总页数
	 */
	private int totalPage = 1;

	/**
	 * 总记录数
	 */
	private long totalCount = 0;// 总条数

	/**
	 * 前一页
	 */
	private int prePage = 1;

	/**
	 * 下一页
	 */
	private int nextPage = 1;

	/**
	 * 是否进入高级搜索
	 */
	private String isSearch = SEARCH_FALSE;

	/**
	 * 返回 pageNum 的值
	 */
	public int getPageNum() {
		if (pageNum > totalPage) {
			pageNum = totalPage;
		}
		return pageNum;
	}

	/**
	 * 设置 pageNum 的值
	 */
	public void setPageNum(final int pageNum) {
		this.pageNum = pageNum > 0 ? pageNum : 1;
		this.plainPageNum = this.pageNum;
	}

	/**
	 * 返回 numPerPage 的值
	 */
	public int getNumPerPage() {
		return numPerPage;
	}

	/**
	 * 设置 numPerPage 的值
	 */
	public void setNumPerPage(final int numPerPage) {
		this.numPerPage = numPerPage > 0 ? numPerPage : 5;
	}

	/**
	 * 返回 orderField 的值
	 */
	public String getOrderField() {
		return orderField;
	}

	/**
	 * 设置 orderField 的值
	 */
	public void setOrderField(final String orderField) {
		this.orderField = orderField;
	}

	/**
	 * 返回 orderDirection 的值
	 */
	public String getOrderDirection() {
		return orderDirection;
	}

	/**
	 * 设置 orderDirection 的值
	 */
	public void setOrderDirection(final String orderDirection) {
		this.orderDirection = orderDirection;
	}

	/**
	 * 返回 totalPage 的值
	 */
	public int getTotalPage() {
		return totalPage;
	}

	/**
	 * 设置 totalPage 的值
	 */
	public void setTotalPage(final int totalPage) {
		this.totalPage = totalPage;
	}

	/**
	 * 返回 prePage 的值
	 */
	public int getPrePage() {
		prePage = pageNum - 1;
		if (prePage < 1) {
			prePage = 1;
		}
		return prePage;
	}

	/**
	 * 设置 prePage 的值
	 */
	public void setPrePage(final int prePage) {
		this.prePage = prePage;
	}

	/**
	 * 返回 nextPage 的值
	 */
	public int getNextPage() {
		nextPage = pageNum + 1;
		if (nextPage > totalPage) {
			nextPage = totalPage;
		}

		return nextPage;
	}

	/**
	 * 设置 nextPage 的值
	 */
	public void setNextPage(final int nextPage) {
		this.nextPage = nextPage;
	}

	/**
	 * 返回 totalCount 的值
	 */
	public long getTotalCount() {
		return totalCount;
	}

	/**
	 * 设置 totalCount 的值
	 */
	public void setTotalCount(final long totalCount) {
		this.totalCount = totalCount;
		totalPage = ((int) (totalCount - 1) / this.numPerPage) + 1;
	}

	/**
	 * 返回 plainPageNum 的值
	 */
	public int getPlainPageNum() {
		return plainPageNum;
	}

	/**
	 * 设置 plainPageNum 的值
	 */
	public void setPlainPageNum(final int plainPageNum) {
		this.plainPageNum = plainPageNum;
	}

	/**
	 * 得到是否进入高级搜索的判断值
	 */
	public String getIsSearch() {
		return isSearch;
	}

	/**
	 * 设置是否进入高级搜索的判断值
	 */
	public void setIsSearch(final String isSearch) {
		this.isSearch = isSearch;
	}

	/**
	 * 增加排序
	 * 
	 * @author 郭浩
	 * @date 2014-10-11
	 */
	public  void addSort(final String fieldName, final String sortType) {
		sortFields.add(new FieldSort(fieldName, sortType));

	}

	public  List<FieldSort> getSortFields() {
		return sortFields;
	}

	public  boolean hasSort() {
		return sortFields.size() > 0;
	}
}
